package com.yilu.driver.ui.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.is.ui.selectrecyclerview.DragSelectRecyclerViewAdapter;
import com.is.ui.widget.RectangleView;
import com.yilu.driver.R;
import com.yilu.driver.entity.StudentApplyResponse;

import java.util.List;

/**
 * @author Administrator
 * @date 2017/4/18 0018
 */

public class ApplyExamSelectAdapter extends DragSelectRecyclerViewAdapter<ApplyExamSelectAdapter.MainViewHolder> {
    private List<StudentApplyResponse.DataBean> mDatas;

    public interface ClickListener {
        void onClick(int index);

        void onLongClick(int index);
    }

    private final ClickListener mCallback;

    public ApplyExamSelectAdapter(List<StudentApplyResponse.DataBean> mDatas, ClickListener callback) {
        super();
        mCallback = callback;
        this.mDatas = mDatas;
    }

    public StudentApplyResponse.DataBean getItem(int index) {
        return mDatas.get(index);
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_unapply_exam, parent, false);
        return new MainViewHolder(v, mCallback);
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        holder.txtTraineeName.setText(getItem(position).getName());
        holder.txtArchivesNumber.setText(getItem(position).getArchivesNumber());
        holder.txtMobile.setText(getItem(position).getMobile());
        holder.txtSubject.setText(getItem(position).getSubjects());
        holder.txtCarType.setText(getItem(position).getName());
        final Drawable d;
        final Context c = holder.itemView.getContext();

        if (isIndexSelected(position)) {
            d = new ColorDrawable(ContextCompat.getColor(c, R.color.grid_foreground_selected));
            holder.checkBox.setChecked(true);
        } else {
            d = null;
            holder.checkBox.setChecked(false);
        }

        //noinspection RedundantCast
        ((FrameLayout) holder.colorSquare).setForeground(d);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class MainViewHolder extends RecyclerView.ViewHolder
            implements View.OnClickListener, View.OnLongClickListener {

        public final RectangleView colorSquare;
        private final ClickListener mCallback;
        private TextView txtTraineeName;
        private TextView txtArchivesNumber;
        private TextView txtMobile;
        private TextView txtSubject;
        private TextView txtCarType;
        private CheckBox checkBox;

        public MainViewHolder(View itemView, ClickListener callback) {
            super(itemView);
            mCallback = callback;
            txtTraineeName = (TextView) itemView.findViewById(R.id.txt_trainee_name);
            txtArchivesNumber = (TextView) itemView.findViewById(R.id.txt_trainee_archivesNumber);
            txtMobile = (TextView) itemView.findViewById(R.id.txt_mobile);
            txtSubject = (TextView) itemView.findViewById(R.id.txt_subject);
            txtCarType = (TextView) itemView.findViewById(R.id.txt_car);
            checkBox = (CheckBox) itemView.findViewById(R.id.chk_select);
            this.colorSquare = (RectangleView) itemView.findViewById(R.id.colorSquare);
            this.itemView.setOnClickListener(this);
            this.itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mCallback != null)
                mCallback.onClick(getAdapterPosition());
        }

        @Override
        public boolean onLongClick(View v) {
            if (mCallback != null)
                mCallback.onLongClick(getAdapterPosition());
            return true;
        }
    }
}
